package pqt;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumnModel;

public class AsignarAtributos extends JDialog {

	private final JPanel contentPanel = new JPanel();
	BaseDeDatos db = BaseDeDatos.getInstance();
	final JComboBox listadoBalanzas;
	final ColorTableModel model = new ColorTableModel();
	Balanza balanza[];
	
	public AsignarAtributos(JPanel p) {
		
		p.setLayout(null);
		JPanel panelAsignarAtributos = new JPanel();
		//tabbedPane.addTab("Asignar Atributos", null, panelAsignarAtributos, null);
		panelAsignarAtributos.setLayout(null);
		panelAsignarAtributos.setBounds(0, 0, 1200,700);
		p.add(panelAsignarAtributos);
		
		JPanel panelTabla = new JPanel();
		panelTabla.setBackground(Color.WHITE);
		panelTabla.setBounds(500, 10, 700,550);
		panelAsignarAtributos.add(panelTabla);
		panelTabla.setLayout(null);
		
		JLabel lblSeleccioneBloque = new JLabel("Seleccione Balanaza:");
		lblSeleccioneBloque.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblSeleccioneBloque.setBounds(10, 15, 140, 22);
		panelAsignarAtributos.add(lblSeleccioneBloque);
		
		listadoBalanzas = new JComboBox();
		listadoBalanzas.setBounds(180, 18, 197, 20);
		listarBalanzas();
		panelAsignarAtributos.add(listadoBalanzas);
		cargarAtributos();
		final JTable tabla = new JTable(model);
		//tabla.setBounds(0, 0, 700, 900);
	    resizeColumnWidth(tabla);
	    tabla.getTableHeader().setReorderingAllowed(false);
	    final JScrollPane scrollPane2 = new JScrollPane();
	    scrollPane2.setBounds(0, 0, 700, 550);
	    scrollPane2.setViewportView(tabla);
	    panelTabla.add(scrollPane2);
	   
	    JButton btnTodos = new JButton("Marcar Todos");
		btnTodos.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				for(int j=0;j<model.getRowCount();j++) {
					model.setValueAt(true, j, 2);
					scrollPane2.repaint();
					
				}
			}
		});
		btnTodos.setBounds(500, 570, 120, 23);
		panelAsignarAtributos.add(btnTodos);
		
		JButton btnNinguno = new JButton("Desmarcar Todos");
		btnNinguno.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				for(int j=0;j<model.getRowCount();j++) {
					model.setValueAt(false, j, 2);
					scrollPane2.repaint();
					
				}
			}
		});
		btnNinguno.setBounds(630, 570, 140, 23);
		panelAsignarAtributos.add(btnNinguno);
		
		JButton btnAceptar2 = new JButton("Aceptar");
		btnAceptar2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				try {
					db.asignarAtributosBalanza(balanza[listadoBalanzas.getSelectedIndex()].getId(), model);
					
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		btnAceptar2.setBounds(1000, 620, 89, 23);
		
		panelAsignarAtributos.add(btnAceptar2);
		
		JButton btnCancelar = new JButton("Cancelar");
		btnCancelar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
			}
		});
		btnCancelar.setBounds(1110, 620, 89, 23);
		
		panelAsignarAtributos.add(btnCancelar);
		
		listadoBalanzas.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				tabla.getTableHeader().getColumnModel().getColumn(2).setHeaderValue(listadoBalanzas.getSelectedItem().toString());
				try {
					db.leerAtributosBalanza(model, balanza[listadoBalanzas.getSelectedIndex()]);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				scrollPane2.repaint();
			}
		});
		
		
	}

	private void listarBalanzas() {
			
		try {
			
			balanza = db.consultarBalanzas(false);
			
			System.out.println("lennn:"+balanza.length);
				
			for(int i=0;i<balanza.length;i++) {
				listadoBalanzas.addItem(balanza[i].getCodigo());
				
			}
			
			listadoBalanzas.setSelectedIndex(0);
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	private void resizeColumnWidth(JTable table) {
	    final TableColumnModel columnModel = table.getColumnModel();
	    for (int column = 0; column < table.getColumnCount(); column++) {
	        int width = 50; // Min width
	        for (int row = 0; row < table.getRowCount(); row++) {
	            TableCellRenderer renderer = table.getCellRenderer(row, column);
	            Component comp = table.prepareRenderer(renderer, row, column);
	            width = Math.max(comp.getPreferredSize().width, width);
	        }
	        columnModel.getColumn(column).setPreferredWidth(width);
	    }
	}
	
	public void cargarAtributos() {
		
		try {
			db.obtenerAtributos(model);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	
}
